//
//  JKNewFeatureView.swift
//  新浪微博
//
//  Created by Jack.Lin on 16/7/4.
//  Copyright © 2016年 Jack.Lin. All rights reserved.
//

import UIKit

class JKNewFeatureView: UIView {

    @IBOutlet weak var scrollView: UIScrollView!
    
    
    @IBOutlet weak var enterButton: UIButton!

    @IBOutlet weak var pageControll: UIPageControl!
    
    // 进入微博
    @IBAction func enterStatus() {
        
        removeFromSuperview()
    }
    
    
    
    
    
    class func newFeatureView() -> JKNewFeatureView {
        
        let nib = UINib(nibName: "JKNewFeatureView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! JKNewFeatureView
        
        v.frame = UIScreen.main().bounds
        
        return v
    }
    
    override func awakeFromNib() {
        let count = 4

        let rect = UIScreen.main().bounds
        
        for i in 0..<count {
            let imageName = "new_feature_\(i + 1)"
            let iv = UIImageView(image: UIImage(named: imageName))
            
            iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            
            scrollView.addSubview(iv)
        }
        
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
     
        scrollView.delegate = self
        
        enterButton.isEnabled = true
     
    }
}


extension JKNewFeatureView: UIScrollViewDelegate {

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page == scrollView.subviews.count {
            print("欢迎欢迎, 热泪欢迎!!!")
            removeFromSuperview()
        }
        // 如果是倒数第二页, 显示按钮
        enterButton.isHidden = (page != scrollView.subviews.count - 1)
        
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        // 一旦滚动隐藏按钮
        enterButton.isHidden = true
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        // 设置分页控件
        pageControll.currentPage = page
        
        // 分页控件的隐藏
        pageControll.isHidden = (page == scrollView.subviews.count)
    }
}

